<?php

namespace YmPhp\Admin\Http\Controllers\Setting;

use YmPhp\Admin\Models\BottomNavigation;
use YmPhp\Admin\Form;
use YmPhp\Admin\Show;
use YmPhp\Admin\Http\Controllers\AdminController;
use YmPhp\Admin\Grid;

class BottomNavigationController extends AdminController
{


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BottomNavigation(), function (Grid $grid) {
            $grid->model()->orderBy('sort','asc');
            $grid->column('id')->sortable();
            $grid->column('name')->editable(true);
            $grid->column('model')->display(function ($model) {
                if ($model == 'business'){
                    return '商家';
                }else if ($model == 'user'){
                    return '用户';
                }else if ($model == 'tongcheng'){
                    return '同城信息';
                }else if ($model == 'mall'){
                    return '商城';
                }else if ($model == 'home'){
                    return '首页';
                }
            });
            $grid->column('icon')->image('',40,40);
            $grid->column('icon2')->image('',40,40);
            $grid->column('link')->editable(true);
            $grid->column('protrude')->switch();
            $grid->column('sort')->editable(true);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->showQuickEditButton();
            $grid->disableEditButton();
            $grid->disableViewButton();
            $grid->showRefreshButton();
            $grid->enableDialogCreate();
            $grid->setDialogFormDimensions('50%', '80%');
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('platform', ['h5'=>'H5端','app'=>'APP客户端', 'wxgzh'=>'微信公众号', 'wxmini'=>'微信小程序', 'dymini'=>'抖音小程序']);
                $selector->select('model', ['home'=>'首页','business'=>'商家', 'tongcheng'=>'同城信息', 'mall'=>'商城']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BottomNavigation(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('model');
            $show->field('icon');
            $show->field('icon2');
            $show->field('highlight');
            $show->field('link');
            $show->field('stids');
            $show->field('protrude');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BottomNavigation(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->select('platform')->options(['h5'=>'H5端','app'=>'APP客户端', 'wxgzh'=>'微信公众号', 'wxmini'=>'微信小程序', 'dymini'=>'抖音小程序'])->default('wxmini')->required();
            $form->select('model')->options(['home'=>'首页','business'=>'商家', 'tongcheng'=>'同城', 'mall'=>'商城'])->default('home')->required();
            $form->image('icon')->autoUpload()->uniqueName()->saveFullUrl()->required();
            $form->image('icon2')->autoUpload()->uniqueName()->saveFullUrl()->required();
            $form->url('link')->required();
            $form->switch('protrude');
            $form->text('sort');

            $form->saving(function (Form $form) {
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    $count = (new BottomNavigation())->getCount($form->model);
                    if ($count >= 5) {
                        return $form->response()->error('导航不得超过5个');
                    }
                }
                // 中断后续逻辑
            });
        });
    }

}
